package com.atguigu.tingshu.common.minio.service.impl;


import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.minio.config.MinioProperties;
import com.atguigu.tingshu.common.minio.service.FileOpsService;
import com.atguigu.tingshu.common.util.MD5;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

//@Service
@Slf4j
public class FileOpsServiceImpl implements FileOpsService {
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;



    @Override
    public String fileUpload(MultipartFile file) {
//        String picUrl = "";
        String originalFilename = file.getOriginalFilename();
        try {
//            byte[] bytes = file.getBytes();
//            String fileContentStr = new String(bytes);
//            String filePrefix = MD5.encrypt(fileContentStr);
//            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//            String objectName = filePrefix + fileSuffix;
            //对文件对象进行去重校验
//            StatObjectArgs.Builder statObjectArgsBuilder = StatObjectArgs.builder();
//            StatObjectArgs statObjectArgs = statObjectArgsBuilder
//                    .bucket(minioProperties.getBucketName())
//                    .object(originalFilename)
//                    .build();
//            // 如果不抛出异常，则代表minio指定桶中的文件已经存在，反之对象不存在
//            try {
//                minioClient.statObject(statObjectArgs);
//                log.info("minio指定桶中的对象存在，不用上传，直接返回对象在mino中的地址");
////                throw new GuiguException(201,"图片上传重复");
//                return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + objectName;
//            } catch (ErrorResponseException e) {
//                log.info("minio指定桶中的对象不存在，可以上传");
            // 如果抛出异常，则代表minio指定桶中的文件是第一次上传
            PutObjectArgs.Builder builder = PutObjectArgs.builder();
            PutObjectArgs putObjectArgs = builder
                    .bucket(minioProperties.getBucketName())
                    .object(originalFilename)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);
            //返回地址
            return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + originalFilename;
        } catch (Exception e) {
            log.error("文件上传到minio中失败：{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }



    public void minioDeleteExample(String originalFilename) {
        try {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(minioProperties.getBucketName())
                            .object(originalFilename)
                            .build());
            log.error("图片删除到成功");
        } catch (Exception e) {
            log.error("图片删除失败：{}", e.getMessage());
            throw new GuiguException(201, "图片删除失败");
        }
    }
}
